function rand(min: number, max: number) {
    return min + Math.round(Math.random() * (max - min));
}

export function typing(targetString: string, write: (char: string) => Promise<any>, minTime = 50, maxTime = 500) {
    return new Promise((resolve) => {
        let currentIndex = 0;
        const next = async () => {
            targetString += "";
            if (currentIndex < targetString.length) {
                await write(targetString[currentIndex])
                currentIndex++;
                setTimeout(
                    next.bind(this), // 递归自身，并传参
                    rand(minTime, maxTime)
                );
            } else {
                if (resolve) resolve(true); // 所有字符自增完成
            }
        }
        next();
    });
}